local redis_client = require "lib.redis"

local args = ngx.req.get_uri_args()

if nil == args.id or nil == args.vote_id then
    ngx.say('{"code":10000, "message":"id及vote_id不能为空"}')
    return
end

local item_id = args.id or args.item_id
local vote_id = args.vote_id

local redis = redis_client:new()
local item = redis:get('item:'..vote_id..':'..item_id)

if nil == item then
    ngx.say('{"code":10000, "message":"项目不存在"}')
    return
end

local ranking_key = 'ranking:'..vote_id
local votes = redis:zscore(ranking_key, item_id) or 0
local rank = redis:zrevrank(ranking_key, item_id) or 0
votes = tonumber(votes)
rank = tonumber(rank) + 1

ngx.say('{"data":{"item":', item,',"votes":', votes,',"rank":', rank,'},"code":200,"message":"success"}')
